﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MoocProject
{
	public class CrawFactory : ICrawFactory
	{
		private readonly IOptionsSnapshot<CrawConfig> options;
		public CrawFactory(IOptionsSnapshot<CrawConfig> options)
		{
			this.options = options;
		}
		public IWebDriver GetDriverHeadless(string type)
		{
			DriverOptions driverOptions;

			// 根据浏览器类型创建对应的选项实例
			switch (type.ToLower())
			{
				case "chrome":
					var chromeOptions = new ChromeOptions();
					chromeOptions.AddArgument("--headless");
					driverOptions = chromeOptions;
					break;
				case "edge":
					var edgeOptions = new EdgeOptions();
					edgeOptions.AddArgument("--headless");
					edgeOptions.AddArgument("--disable-gpu");
					edgeOptions.AddArgument("--window-size=1920,1080");
					driverOptions = edgeOptions;
					break;
				case "firefox":
					var firefoxOptions = new FirefoxOptions();
					firefoxOptions.AddArgument("--headless");
					driverOptions = firefoxOptions;
					break;
				case "ie":
					// IE不支持无头模式，使用常规模式
					driverOptions = new InternetExplorerOptions();
					break;
				default:
					throw new ArgumentException($"不支持{type}浏览器");
			}
			driverOptions.AcceptInsecureCertificates = true;

			return type.ToLower() switch
			{
				"chrome" => new ChromeDriver((ChromeOptions)driverOptions),
				"edge" => new EdgeDriver((EdgeOptions)driverOptions),
				"firefox" => new FirefoxDriver((FirefoxOptions)driverOptions),
				"ie" => new InternetExplorerDriver((InternetExplorerOptions)driverOptions),
				_ => throw new ArgumentException($"不支持{type}浏览器")
			};
		}
		public IWebDriver GetDriver(string type)
		{
			DriverOptions driverOptions;

			// 根据浏览器类型创建对应的选项实例
			switch (type.ToLower())
			{
				case "chrome":
					driverOptions = new ChromeOptions();
					break;
				case "edge":
					driverOptions = new EdgeOptions();
					break;
				case "firefox":
					driverOptions = new FirefoxOptions();
					break;
				case "ie":
					driverOptions = new InternetExplorerOptions();
					break;
				default:
					throw new ArgumentException($"不支持{type}浏览器");
			}
			driverOptions.AcceptInsecureCertificates = true;
			return type.ToLower() switch
			{
				"chrome" => new ChromeDriver((ChromeOptions)driverOptions),
				"edge" => new EdgeDriver((EdgeOptions)driverOptions),
				"firefox" => new FirefoxDriver((FirefoxOptions)driverOptions),
				"ie" => new InternetExplorerDriver((InternetExplorerOptions)driverOptions),
				_ => throw new ArgumentException($"不支持{type}浏览器")
			};
		}
		public ICrawService GetCraw(string type) => new CrawService(GetDriver(type));
		public ICrawService GetCrawHeadless(string type)=>new CrawService(GetDriverHeadless(type));
	}
}
